
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import watij.elements.*;
import watij.runtime.ie.*;

/**
 * Servlet implementation class ReturnNoOfElement
 */
public class ReturnNoOfElement extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ReturnNoOfElement() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		PrintWriter pw = response.getWriter();
		IE ie = new IE();
		String url = request.getParameter("url");
		String name = request.getParameter("name");
		String type = request.getParameter("type");
		String string1= "[";
		try
		{
		ie.start(url);
		int i;
		if(type.equals("Radio"))
		{
			int count = 0;
			for(i=0;i<ie.radios().length();i++)
			{
				if(ie.radio(i).name().equals(name))
					{
					count++;
					string1 += "{'cmpname':'"+ie.radio(i).value()+"','cmptype':'Radio'}";
					}
					
			}
			string1 += "]";
			String jsonResponse = "{'success':'true','size':'"+count+"','data':"+string1+"}";
			pw.println(jsonResponse);
		}
		
		else if(type.equals("CheckBox"))
		{
			int count = 0;
			for(i=0;i<ie.checkboxes().length();i++)
			{
				if(ie.checkbox(i).name().equals(name))
				{
					count++;
					string1 += "{'cmpname':'"+ie.checkbox(i).value()+"','cmptype':'CheckBox'}";
				}
			}
			string1 += "]";
			String jsonResponse = "{'success':'true','size':'"+count+"','data':"+string1+"}";
			pw.println(jsonResponse);
		}
		else if(type.equals("SelectList"))
		{
			int count = 0;
			for(i=0;i<ie.selectLists().length();i++)
			{
				if(ie.selectList(i).name().equals(name))
				{
					for(int j=0;j<ie.selectList(i).options().length();j++)
					{
						count++;
						string1 += "{'cmpname':'"+ie.selectList(i).option(j).value()+"','cmptype':'SelectList'}";
					}
				}
			}
			string1 += "]";
			String jsonResponse = "{'success':'true','size':'"+count+"','data':"+string1+"}";
			pw.println(jsonResponse);
		}
		else if(type.equals("Hidden"))
		{
			for(i=0;i<ie.hiddens().length();i++)
				if(ie.hidden(i).name().equals(name))
					{
					string1 += "{'cmpname':'"+ie.hidden(i).value()+"','cmptype':'Hidden'}";
					break;
					}
			string1 += "]";
			String jsonResponse = "{'success':'true','size':'1','data':"+string1+"}";
			pw.println(jsonResponse);
		}
		ie.close();
		}
		catch(Exception ex)
		{
			pw.print(ex.getMessage());
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}